/**
 * 
 */
package com.youxin.cpcenter.utils;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.youxin.busfoundation.bean.BusinessMessages;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.ent.RequestSwitchBean;
import com.youxin.busfoundation.bean.ent.ResponseAccountBean;
import com.youxin.busfoundation.bean.ent.ResponseAccountsBean;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.BusinessDataException;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.common.redis.RedisOperator;
import com.youxin.cpcenter.dao.TbEntMemberInfoMapper;
import com.youxin.cpcenter.dao.TbMemberInfoMapper;
import com.youxin.cpcenter.model.TbEntMemberInfo;
import com.youxin.cpcenter.model.TbMemberInfo;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.JsonUtils;
import com.youxin.foundation.utils.StringUtils;

/**
 * @author yingde.cao
 * @date 2017年12月10日
 * @version 1.0
 */
public class LoginUtils {
	protected static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(LoginUtils.class);

	/**
	 * 登录
	 * @description 
	 * @param tbMemberInfoMapper
	 * @param tbEntMemberInfoMapper
	 * @param mobile
	 * @return
	 */
	public static UserInfo loginWithMobile(TbMemberInfoMapper tbMemberInfoMapper
			,TbEntMemberInfoMapper tbEntMemberInfoMapper
			,String mobile)
	{
		TbMemberInfo memberInfo=tbMemberInfoMapper.selectByMobile(mobile);
		if(memberInfo==null)
		{
			throw new BusinessDataException("手机号码没有注册");
		}		
		TbEntMemberInfo entMemberInfo=null;
		if(memberInfo.getEntMemberId().longValue()>0L)
		{
			entMemberInfo=tbEntMemberInfoMapper.selectByPrimaryKey(memberInfo.getEntMemberId());
		}				
		
		if(entMemberInfo==null)
		{			
			logger.error("loginWithMobile:企业会员不存在:"+String.valueOf(JsonUtils.objectToFasterxmlJsonString(memberInfo)));
			throw new BusinessDataException("企业会员不存在");
		}else{
			//entMemberInfo.getEnterprisePass().shortValue()!=EnterprisePassStatus.Pass.getValue()
			if(entMemberInfo.isPassed()==false){
				logger.error("loginWithMobile:没有开通企业版:"+String.valueOf(JsonUtils.objectToFasterxmlJsonString(entMemberInfo)));
				throw new BusinessDataException("没有开通企业版");
			}
		}
		
		UserInfo userInfo=new UserInfo();
		userInfo.setIdentify(entMemberInfo);
		userInfo.setId(memberInfo.getId());
		userInfo.setPosition(memberInfo.getPosition());
		userInfo.setUsername(memberInfo.getUsername());
		userInfo.setUid(memberInfo.getUid());
		userInfo.setPhone(memberInfo.getPhone());
		userInfo.setEmail(memberInfo.getEmail());
		userInfo.setRealname(memberInfo.getRealname());					
		String result=RedisOperator.saveUserInfo(userInfo);	
		if(RedisClusterCommand.isRedisOk(result))
		{
			return userInfo;
		}
		return null;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public static ResponseObject switchAccount(RequestSwitchBean bean,TbMemberInfoMapper tbMemberInfoMapper
			,TbEntMemberInfoMapper tbEntMemberInfoMapper)
	{
		UserInfo userInfo=LoginAuth.getUserInfo();		
		List<TbEntMemberInfo> entaccounts=tbEntMemberInfoMapper.selectAllByMemberId(userInfo.getId());
		//List<ResponseAccountBean> accounts=tbEntMemberInfoMapper.selectByMemberId(userInfo.getId());	
		TbEntMemberInfo entMemberInfo=null;
		for(TbEntMemberInfo item:entaccounts){
			if(item.getId().longValue()==bean.getEntMemberId())
			{				
				entMemberInfo=item;				
				break;
			}
		}	
		if(entMemberInfo==null)
		{
			return ResponseObject.userFailure(BusinessMessages.ACCOUNT_SWITCH_FAILURE);
		}
		UserInfo updateUserInfo=new UserInfo();
		BeanUtils.copyProperties(userInfo, updateUserInfo);
		updateUserInfo.setIdentify(entMemberInfo);
		
		TbMemberInfo memberInfo=new TbMemberInfo();
		memberInfo.setId(entMemberInfo.getMemberId());
		memberInfo.setEntMemberId(entMemberInfo.getId());
		int rowEffected=tbMemberInfoMapper.updateByPrimaryKeySelective(memberInfo);
		if(rowEffected==0)
		{
			throw new BusinessDataException(BusinessMessages.ACCOUNT_SWITCH_FAILURE);
		}
		String result=RedisOperator.saveUserInfo(updateUserInfo);	
		if(!RedisClusterCommand.isRedisOk(result))
		{
			throw new BusinessDataException(BusinessMessages.ACCOUNT_SWITCH_FAILURE);
		}
		ResponseAccountsBean responseAccountsBean=new ResponseAccountsBean();
		List<ResponseAccountBean> accounts=entaccounts.stream().map((t)->{
			ResponseAccountBean item=new ResponseAccountBean();
			item.setEntMemberId(t.getId());	
			item.setMemberId(t.getMemberId());
			item.setCompanyName(t.getCompanyName());
			item.setEnterpriseManager(t.getEnterpriseManager());
			if(t.getId().longValue()==bean.getEntMemberId())
			{
				item.setCurrent(1);
				copyAccountInfo(item,responseAccountsBean);
			}
			return item;
		}).collect(Collectors.toList());
		responseAccountsBean.setAccounts(accounts);		
		return ResponseObject.successObject(responseAccountsBean);
	}
	/**
	 * 拷贝当前登陆信息
	 * @description 
	 * @param currentItem
	 * @param responseAccountsBean
	 */
	public static void copyAccountInfo(ResponseAccountBean currentItem,ResponseAccountsBean responseAccountsBean)
	{
		responseAccountsBean.setCompanyName(currentItem.getCompanyName());
		responseAccountsBean.setEnterpriseManager(currentItem.getEnterpriseManager());
		responseAccountsBean.setCurrent(1);
		responseAccountsBean.setMemberId(currentItem.getMemberId());
		responseAccountsBean.setEntMemberId(currentItem.getEntMemberId());
		responseAccountsBean.setEnterpriseIdentyType(currentItem.getEnterpriseIdentyType());
	}
	
}
